// JavascriptGameLib :: CORE
// Rewritten by Andrew Wooldridge 2008 - integrate with YUI.
// Modified by Brent Silby 2002,2003. Now uses setTimeout for loop
// rather than setInterval. This fixes speed inconsistency on win98

YAHOO.namespace("Gamelib");

(function(){
	YAHOO.Gamelib.sprite = function(){
		//private methods
		
		//public methods
		return {
			totalsprites: 0,
			xoffset: 0,
			yoffset:0
			
			
		}
		
	}();
	YAHOO.Gamelib.core = function(){
		//private methods

		
		//public methods
		return {
			version: 3.0,
			n: 0,
			STT:null,
			speedTestDiv:null,
			speedTestFinish:false,
			callBack:'',
			fps:0,
			layers:[],
			totalpreimages:0,
			interval:null,
			hooked:[],
			preimage:[],
			ticker:0,
			sin:[],
			cos:[],
			cookies:[],
			layer_index:0,
			iebuffer:null,
			ieframe:null,
			firstlayer:true,
			f:[],
			flength:0,
			timerSpeed:40,
			browser:null,
			onSpecialItem:false,
			loadQueue:[],
			loadwait:null,
			loadIndex:-4,
			loading:false,
			loadTimer:null,
			loadCL:null,
			loadTime:0,
			running:false,
			widgetsInUse:false,
			
			preloader: function(){
				var n,x;
				var ygc = YAHOO.Gamelib.core;
				for(n=0;n<arguments.length;n++){
					x=ygc.totalpreimages++;
					ygc.preimage[x]=new Image;
					ygc.preimage[x].src=arguments[n]
				}	
			},
			
			hook: function(fnc){
				var ygc = YAHOO.Gamelib.core;
				ygc.hooked[ygc.hooked.length] = fnc;
			},
			
			unhook: function(){
				var ygc = YAHOO.Gamelib.core;
				var fnd = false;
				for(var n=0;n<ygc.hooked.length;n++){
					if(ygc.hooked[n]==fnc){
						fnd=true;
						break;
					}					
				}
				if(fnd){
					for(var i=n; i<ygc.hooked.length-1;i++){
						ygc.hooked[i]=ygc.hooked[i+1];	
					}
					ygc.hooked.length--;
				}
			},
			start: function(){
				var ygc = YAHOO.Gamelib.core;
				if (ygc.browser.panic) {
					ygc.alert('ygc.start()','Error! JavascriptGamelib could not start!');
					return;
				}
				if(!ygc.running){
					ygc.running = true;
					if(ygc.ns4){
						ygc.interval=setTimeout("YAHOO.Gamelib.core.loop()",ygc.timerSpeed);
					}else{
						ygc.loop();
					}
				}
			},
			stop: function(){
				var ygc = YAHOO.Gamelib.core;
				ygc.running = false;
				clearTimeout(ygc.interval);
			},
			setTimerSpeed: function(x){
				var ygc = YAHOO.Gamelib.core;
				ygc.stop();
				ygc.timerSpeed = x;
				ygc.start();
			},
			makedegs: function(){
				var ygc = YAHOO.Gamelib.core;
				for(var n=0;n<360;n++){
					console.log('ygc.makedegs ', n)
                   ygc.sin[n]=(Math.sin(3.14159*n/180));
                   ygc.cos[n]=-(Math.cos(3.14159*n/180))
                }
			},
			get_cookies: function(){
				var ygc = YAHOO.Gamelib.core;
				var tar=document.cookie.split("; ");
				for(var n=0;n<tar.length;n++){
					ygc.cookies[n]= new ygc.Cookie_construct(tar[n]);
				}
			},
			Cookie_construct:function(gl_var){
				var tmp=gl_var.split("=");
				var retval = {};
				retval.name=unescape(tmp[0]);
				retval.value=unescape(tmp[1]);
				return retval;
			},
			set_cookie: function(gl_value){
				var expiresTime = new Date();
				expiresTime.setTime(expiresTime.getTime() + 999999*1000);
				document.cookie = this.name + "=" + escape(gl_value) + "; expires=" + expiresTime.toGMTString() +"; path=/";
				this.value=gl_value;
				return true
			},
			delete_cookie: function(){
				var ygc = YAHOO.Gamelib.core;
				for(var n=0;n<ygc.cookies.length;n++){
					if(ygc.cookies[n].name==this.name){
						this.value=null;
						document.cookie=this.name+"=; expires= Mon, 3 Jan 2000 00:00:00 UTC; path=/";
						ygc.cookies[n].name="";
						ygc.cookies[n].value="";
						break
					}
				}
				return true
			},
			get_cookie: function(c_name){
				var ygc = YAHOO.Gamelib.core;
				for(var n=0;n<ygc.cookies.length;n++){
					if(ygc.cookies[n].name==c_name){
						return ygc.cookies[n].value
					}
				}
				return null;
				
			},
			cookie: function(gl_name){
				var ygc = YAHOO.Gamelib.core;
				return{
					name: gl_name,
					value:ygc.get_cookie(gl_name),
					setvalue:ygc.set_cookie,
					erase:ygc.delete_cookie
					
				}
				
			},
			
						
		}
		
	}();
	/** after gamelib core **/
	
	
})();